<?php

function create_alltorrent_table($content = "")  {

	global $post, $wp_version;
	
	if (!is_page()) {
		return $content;
	}
	
	$search="/wpbt_all_torrents_table/";
	preg_match($search, $content, $matches);
	
	if (!$matches) {
			return $content;
	}
	
	// get all .torrent attachments from all posts
	$tattachments = get_posts( 
				array('numberposts'	=> -1,
					'post_type'	=> 'attachment',
					'post_status' => null,
					'post_parent' => null,
					'post_mime_type' => 'application/x-bittorrent'
				)
			);
			
	if (!$tattachments) {
			return $content;
	}

	if(!$_GET['paged']) {
		$_GET['paged'] = '1';
	}

	$limit = 10;
	
	$curr_page = $_GET['paged'];

	$page_links = paginate_links( 
		array(
			'base' => add_query_arg( 'paged', '%#%' ),
			'format' => '',
			'prev_text' => __("&#9668 Prev"),
			'next_text' => __("Next &#9658"),
			'total' => ceil(count($tattachments)/$limit),
			'current' => $_GET['paged']
		)
	);

	$pattachments = array_chunk($tattachments, $limit);

	$output = "\n<div class=\"wpbt\">\n";

	$output .= "<table class=\"wpbt_all_ttable\">";
	$output .= "<thead> <tr>";
	$output .= "<th> Post Title </th>";
	$output .= "<th> DL </th>";
	$output .= "<th> Size </th>";
	$output .= '<th><img src="/wp-content/plugins/wpbtlite/images/arrowup.gif" alt="Up" title="Uploaders"></th>';
	$output .= '<th><img src="/wp-content/plugins/wpbtlite/images/arrowdown.gif" alt="Down" title="Downloaders"></th>';
	$output .= '<th><img src="/wp-content/plugins/wpbtlite/images/complete.gif" alt="Done" title="Completed"></th>';
	$output .= '<th><img src="/wp-content/plugins/wpbtlite/images/files.gif" alt="Files" title="Files"></th>';
	$output .= "</tr> </thead>";

	foreach ($pattachments[$curr_page-1] as $tattachment) {
	
		$file_link = wp_get_attachment_url($tattachment->ID);
  		$file_name_array = explode("/",$file_link);
  		$file_name = array_reverse($file_name_array);
  	
  		// Get File metadata from DB
  		$tfile_metadata = wp_get_attachment_metadata($tattachment->ID);
  		
  		$output .= '<tr> <td align=\"left\"> <a href="'. get_permalink($tattachment->post_parent) .'">'. get_the_title($tattachment->post_parent) .'</a> </td>';
  		$output .= '<td align=\"center\">';
  		$output .= '<a href="'. $file_link.'"><img src="/wp-content/plugins/wpbtlite/images/download.gif" alt="DL" title="Download This Torrent"></a>';
  		$output .= '</td> <td>';
  		$output .=  mksize($tfile_metadata['size']);
  		$output .= '</td> <td>';
		$output .=  $tfile_metadata['complete'];
  		$output .= '</td> <td>';
  		$output .=  $tfile_metadata['incomplete'];
  		$output .= '</td> <td>';
		$output .=  $tfile_metadata['downloaded'];
  		$output .= '</td> <td>';
		$output .=  count($tfile_metadata['files']);
  		$output .= '</td> ';  	

  		$output .= '</tr>';  	

	}
  	$output .= "</table>";		
		
		if (count($tattachments) >= $limit)	{
			$output .= "<table> <td align=\"center\">";
			$output .= $page_links; 
			$output .= "</td> </table>";
		}
		
  	$output .= "</div>"; 	
  		
//	echo $output;
	
	$content = preg_replace ($search, $output, $content);
	
	return $content;
	}
	
?>
